import Vue from 'vue'
import Router from 'vue-router'
import store from '@/common/store/index.js'
import index from '@/view/index'
import user_login from '@/view/user_login'

Vue.use(Router)
const router = new Router({
        routes: [{
                path: '/',
                component: user_login,
                meta: {
                    requireAuth: false
                }
            },
            {
                path: '/index',
                component: index,
                meta: {
                    requireAuth: false
                },
                children: [],
            },
            {
                path: '/forgetPwd',
                component: resolve => require(['../view/forget_pwd.vue'], resolve),
                name: '忘记密码',
            },
            {
                path: '/otherLogin',
                component: resolve => require(['../view/other_login.vue'], resolve),
                name: '第三方绑定手机号',
            },
        ]
    })
    // 点击当前路由时
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

// 判断是否需要登录权限 以及是否登录
router.beforeEach((to, from, next) => {
    if (to.matched.some(res => res.meta.requireAuth)) { // 判断是否需要登录权限
        if (localStorage.getItem('token')) { // 判断是否登录
            next()
        } else {
            next({
                path: '/',
                query: {
                    redirect: to.fullPath
                }
            })
        }
    } else {
        next()
    }
})

export default router